|Tools| <Simple functions in yassl| |Up to Language Overview | |Simple spreadsheet>

Language Overview Lexical binding


5.2.3 Lexical binding

Like Scheme, yassl implements lexical binding. This easiest to describe through an example. This script creates buttons that flip their labels each time they are pressed. This is the script that does the task
proc Button mkButton(String flip, String flop)
{
  Button ret = new Button;
  boolean state = true;
  ButtonLabel(ret, flop);
  proc void flip_proc()
    {
      if (state)
        { ButtonLabel(ret, flip); state = false; }
      else
        { ButtonLabel(ret, flop); state = true; }
    }
  SetAction(ret, flip_proc);
  return (ret);
}

Add(thisApplet, mkButton('to be', 'not to be'));
Add(thisApplet, mkButton('procedural', 'functional'));

Notice that procedures can be nested. Lexical binding means that the procedures "capture" the values of any free variables from when the procedure was defined. All the variables in flip_proc are free, so it captures them with the values that were present when flip_proc was defined.
|Tools| <Simple functions in yassl| |Up to Language Overview | |Simple spreadsheet>

KB Sriram
Comments, bug reports: kbs@sbktech.org

Revised: Sat May 25 02:58:14 1996
URL: http://www.sbktech.org/yas_lex.html